package part_2;

import java.util.ArrayList;

import genetic_algorithm.Chromosome;

/**
 * this class represents a chromosome for the tsp problem -
 * the basic representation - an array of integers (were 1 stand for city number one and so on....)
 */
public class TspChromosome extends Chromosome
{

	// an array for holding the positions of the cities in the tsp problem
	public static ArrayList<int[]> cities_locs;
	// this chromosome selected route for the salesman to go through
	public ArrayList<Integer> route;
	

	/**
	 * this class default constructor
	 * @param route - this chromosome selected route
	 */
	public TspChromosome(ArrayList<Integer> route)
	{
		this.route=route;
	}

	/**
	 * this function calculate the fitness of this chromosome by
	 * returning the negative number of this chromosome route weight
	 */
	@Override
	public double evaluateFitness() 
	{
		double price = 0.0;
		double cur_dist;
		int[] a;
		int[] b;
		// for every two cities in the route 
		for(int i=0;i<route.size()-1;i++)
		{
			a=cities_locs.get(route.get(i)-1);
			b=cities_locs.get(route.get(i+1)-1);

			// calculate the distance between those two cities
			cur_dist=Math.sqrt(Math.pow(a[0]-b[0],2)+Math.pow(a[1]-b[1],2));
			// and add this distance to the total route weight
			price +=  cur_dist;
		}

		// one more iteration because this is a circle (according to the tsp problem)
		a=cities_locs.get(route.get(0)-1);
		b=cities_locs.get(route.get(route.size()-1)-1);
		price += Math.sqrt(Math.pow(a[0]-b[0],2)+Math.pow(a[1]-b[1],2));
		// return the negative weight - the bigger the weight of the route the lower the fitness of this chromosome
		return (-1)*price;
	}

	/**
	 * this function return this chromosome data - the salesman route
	 */
	@Override
	public ArrayList<Integer> getData() 
	{
		
		return route;
	}

}
